package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.PhysiqueAssessmentDao;
import com.itheima.dao.PhysiqueDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.PhyQuestion;
import com.itheima.pojo.Physique;
import com.itheima.pojo.PhysiqueAssessment;
import com.itheima.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 检查组CRUD操作业务实现类
 *
 * @author W.J.Y
 */
@Service(version = "1.0.0", interfaceClass = PhysiqueService.class)
@Transactional
public class PhysiqueServiceImpl implements PhysiqueService {


    @Resource
    private PhysiqueDao physiqueDao;
    @Resource
    private PhysiqueAssessmentDao physiqueAssessmentDao;


    @Override
    public Result submitScore(PhysiqueAssessment physiqueAssessment) {

        List<Physique> physiqueList = physiqueAssessment.getPhysiqueList();
        Map<String, Float> conversionScoreList = new HashMap<>();
//        List<Float>  conversionScoreList = new ArrayList<>();
        for (Physique physique : physiqueList) {//体质列表

            //questionList 是每一类体质的 所有题目和答案列表
            float allScore = 0;

            List<PhyQuestion> questionList = physique.getQuestionList();
            float count = questionList.size();
            for (PhyQuestion phyQuestion : questionList) {
                //判断以*为结尾的题目 逆向算分
                if ("平和体质".equals(physique.getName())) {
                    String subject = phyQuestion.getSubject();
                    if (subject.endsWith("*")) {
                        if(phyQuestion.getAnswer()>0){

                        phyQuestion.setAnswer(6 - phyQuestion.getAnswer());
                        }
                    }
                }

                //获得每一个题目 计算分数
                float score = phyQuestion.getAnswer();
                allScore += score;
            }

            //算完总分后算转换分
            float conversionScore = 0;
            try {
                conversionScore = (allScore - count) / (count * 4) * 100;
            } catch (Exception e) {
                conversionScore = 0;
            }
            //将对应的体质名 - 转换分放入map

            conversionScoreList.put(physique.getName(), conversionScore);
        }
        Float yangXu = conversionScoreList.get("阳虚体质");
        physiqueAssessment.setYangXuFen(yangXu);

        Float yinXu = conversionScoreList.get("阴虚体质");
        physiqueAssessment.setYinXuFen(yinXu);

        Float qiXu = conversionScoreList.get("气虚体质");
        physiqueAssessment.setQiXuFen(qiXu);

        Float tanShi = conversionScoreList.get("痰湿体质");
        physiqueAssessment.setTanShiFen(tanShi);

        Float shiRe = conversionScoreList.get("湿热体质");
        physiqueAssessment.setShiReFen(shiRe);

        Float XueYu = conversionScoreList.get("血瘀体质");
        physiqueAssessment.setXueYuFen(XueYu);

        Float teBing = conversionScoreList.get("特禀体质");
        physiqueAssessment.setTeBingFen(teBing);

        Float qiYu = conversionScoreList.get("气郁体质");
        physiqueAssessment.setQiYuFen(qiYu);

        Float pinHe = conversionScoreList.get("平和体质");
        physiqueAssessment.setPinHeFen(pinHe);


        if (pinHe >= 60 && yangXu < 40 && yinXu < 40 && qiXu < 40 && tanShi < 40 && shiRe < 40 && XueYu < 40 && teBing < 40 && qiYu < 40) {
            physiqueAssessment.setPinHe(1);
        } else {
            //1为是 0为否
            if (yangXu >= 40) {
                physiqueAssessment.setYangXu(1);
            }
            ;
            if (yinXu >= 40) {
                physiqueAssessment.setYinXu(1);
            }
            ;
            if (qiXu >= 40) {
                physiqueAssessment.setQiXu(1);
            }
            ;
            if (tanShi >= 40) {
                physiqueAssessment.setTanShi(1);
            }
            ;
            if (shiRe >= 40) {
                physiqueAssessment.setShiRe(1);
            }
            ;
            if (XueYu >= 40) {
                physiqueAssessment.setXueYu(1);
            }
            ;
            if (teBing >= 40) {
                physiqueAssessment.setTeBing(1);
            }
            ;
            if (qiYu >= 40) {
                physiqueAssessment.setQiYu(1);
            }
        }
        Date regTime = new Date();
        physiqueAssessment.setRegTime(regTime);
        physiqueAssessmentDao.addPhysiqueAssessment(physiqueAssessment);
        return new Result(true, "新增体质评估成功", null);
    }

    /**
     * 查询所有体质
     *
     * @return 体质集合
     */
    @Override
    public List<Physique> findAllPhy() {
        List<Physique> physiqueList = physiqueDao.findAllPhy();
        for (Physique physique : physiqueList) {
            List<PhyQuestion> phyQuestionList = physiqueDao.findPhyQuestionByPid(physique.getId());
            physique.setQuestionList(phyQuestionList);
        }
        return physiqueList;
    }


    /**
     * 新增检查组
     * @param checkGroup 检查项实例
     */
   /* @Override
    public void add(CheckGroup checkGroup) throws BusinessException {
        Integer count = checkGroupDao.findByNameOrCode(checkGroup);
        if(count>0){
            throw new BusinessException("已存在相同编号、助记码或者名称的检查组，添加失败！");
        }
        //首先添加检查组
        checkGroupDao.add(checkGroup);
        //再添加检查组与检查项的关联
        Integer id = checkGroup.getId();
        List<Integer> checkItemIds = checkGroup.getCheckItemIds();
        for (Integer checkItemId : checkItemIds) {
            checkGroupDao.setCheckGroupAndCheckItem(id,checkItemId);
        }
    }

    *//**
     * 编辑检查组
     * @param checkGroup 检查组实例
     *//*
    @Override
    public void update(CheckGroup checkGroup) throws BusinessException {
        Integer count = checkGroupDao.findByNameOrCode(checkGroup);
        if(count>0){
            throw new BusinessException("已存在相同编号、助记码或者名称的检查组，编辑失败！");
        }
        checkGroupDao.update(checkGroup);
        //首先删除检查组的旧的关联
        checkGroupDao.deleteCheckItemsByCheckGroupId(checkGroup.getId());
        //再添加检查组与检查项的关联
        Integer id = checkGroup.getId();
        List<Integer> checkItemIds = checkGroup.getCheckItemIds();
        for (Integer checkItemId : checkItemIds) {
            checkGroupDao.setCheckGroupAndCheckItem(id,checkItemId);
        }
    }

    *//**
     * 根据ID查询检查组
     * @param id    检查组ID
     * @return 检查组实例
     *//*
    @Override
    public CheckGroup findById(int id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        checkGroup.setCheckItemIds(checkGroupDao.findCheckItemIdsByCheckGroupId(id));
        return checkGroup;
    }




    *//**
     * 删除检查组
     * @param id 检查组ID
     *//*
    @Override
    public void delete(int id) throws BusinessException {
        //查询该检查项是否被检查套餐使用
        int count = checkGroupDao.countByIdFromCheckgroup(id);
        if (count>0){
            throw new BusinessException("该检查组已被检查套餐占用，删除失败！");
        }
        checkGroupDao.delete(id);
    }

    *//**
     * 分页查询
     * @param pageNum  查询页码
     * @param pageSize  每页显示条数
     * @param queryString   检查组编码、助记码或者名称
     * @return 分页查询返回结果
     *//*
    @Override
    public PageResult findPage(int pageNum, int pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用dao层查询语句
        Page<CheckGroup> page = checkGroupDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(),page.getResult());
    }*/
}
